package com.xmz.service.Impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmz.entity.Student;
import com.xmz.entity.StudentExample;
import com.xmz.mapper.StudentMapper;
import com.xmz.service.StudentService;
import com.xmz.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xmz.util.PageResult;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public List<Student> selectAll() {

        return studentMapper.selectByExample(null);
    }

    /*
        分页查询
     */
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
       List<Student> studentList = selectAll();
       PageInfo<Student> info = new PageInfo<>(studentList);
        return new PageResult<Student>(info.getList(), info.getTotal());
    }

    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVo studentVo) {
        List<Student> students = null;
        if(studentVo != null){
            StudentExample example = new StudentExample();
            StudentExample.Criteria criteria =  example.createCriteria();
            if(StrUtil.isNotBlank( studentVo.getName())){
                criteria.andNameLike("%"+studentVo.getName()+"%");
            }
            if(StrUtil.isNotBlank(studentVo.getScalss())){
                criteria.andSclassEqualTo(studentVo.getScalss());
            }
            if(studentVo.getAge() != null){
                criteria.andAgeEqualTo(studentVo.getAge());
            }
            students = studentMapper.selectByExample(example);
        }
        PageResult<Student> pageResult = new PageResult<>();
        pageResult.setRows(students);
        pageResult.setTotal((long) students.size());
        return pageResult;
    }

    @Override
    public int addStudent(Student student) {
        int i = studentMapper.insert(student);
        return i;
    }
}
